package com.zhida.jiyangcenter.setting

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import chooongg.base.annotation.ContentLayout
import chooongg.base.annotation.TitleBar
import chooongg.base.manager.app
import chooongg.base.utils.resColor
import chooongg.base.utils.startActivity
import chooongg.base.utils.toast
import com.flyco.animation.BounceEnter.BounceTopEnter
import com.flyco.animation.SlideExit.SlideBottomExit
import com.flyco.dialog.listener.OnBtnClickL
import com.flyco.dialog.widget.NormalDialog
import com.yanzhenjie.permission.AndPermission
import com.zhida.jiyangcenter.R
import com.zhida.jiyangcenter.account.LoginPassActivity
import com.zhida.jiyangcenter.address.AddressListActivity
import com.zhida.jiyangcenter.mian.BaseActivity
import com.zhida.jiyangcenter.utils.CleanDataUtils
import com.zhida.jiyangcenter.utils.SP
import com.zhida.jiyangcenter.utils.SPUtils
import kotlinx.android.synthetic.main.activity_setting.*

@TitleBar(false)
@ContentLayout(R.layout.activity_setting)
class SettingActivity : BaseActivity() {
    override fun initConfig(savedInstanceState: Bundle?) {
        title_bar.title("设置").navigationIcon(R.drawable.iv_back_fill) {
            finish()
        }
        // 新消息通知
        btn_message.setOnClickListener {
            /** * 跳到通知栏设置界面 * @param context */
            val localIntent = Intent()

            //直接跳转到应用通知设置的代码：
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
                localIntent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
                localIntent.putExtra("app_package", this@SettingActivity.packageName)
                localIntent.putExtra("app_uid", this@SettingActivity.applicationInfo.uid)
            } else if (Build.VERSION.SDK_INT == Build.VERSION_CODES.KITKAT) {
                localIntent.action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
                localIntent.addCategory(Intent.CATEGORY_DEFAULT)
                localIntent.data = Uri.parse("package:" + this@SettingActivity.packageName)
            } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                //                    localIntent.setAction(ACTION_APPLICATION_SETTINGS);
                //                    localIntent.putExtra("app_package", getPackageName());
                //                    localIntent.putExtra("app_uid", getApplicationInfo().uid);
                localIntent.action = "android.settings.APP_NOTIFICATION_SETTINGS"
                localIntent.putExtra(
                    "android.provider.extra.APP_PACKAGE",
                    this@SettingActivity.packageName
                )
            } else { //4.4以下没有从app跳转到应用通知设置页面的Action，可考虑跳转到应用详情页面,
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                localIntent.action = "android.settings.APPLICATION_DETAILS_SETTINGS"
                localIntent.data = Uri.fromParts("package", this@SettingActivity.packageName, null)
            }
            startActivity(localIntent)
        }
        btn_account_safety.setOnClickListener {
            startActivity<AccountSafetyActivity> {  }
        }
        btn_bank_car.setOnClickListener {
            startActivity<AddressListActivity> { }
        }
        btn_real_name.setOnClickListener {
            startActivity<UserInfoActivity> { }
        }

        btn_login_out.setOnClickListener {
            val dialog = NormalDialog(context)
            dialog.content("退出后将不能看到订单信息,确认退出吗？")
                .style(NormalDialog.STYLE_TWO)//
                .title("温馨提示")
                .btnNum(2)
                .btnTextColor(resColor(R.color.colorAccent), resColor(R.color.colorAccent))
                .btnText("确认", "取消")
                .titleTextSize(23f)
                .showAnim(BounceTopEnter())
                .dismissAnim(SlideBottomExit())
                .show()
            dialog.setOnBtnClickL(OnBtnClickL {
                dialog.dismiss()
                clearUserInfo()
                finish()
            }, OnBtnClickL {
                dialog.dismiss()
            })

        }
        // 清理缓存
        btn_cache_clear.setOnClickListener {
            if (CleanDataUtils.clearAllCache(this)) {
                toast("清理成功")
                tv_cache_size.text = CleanDataUtils.getTotalCacheSize(this)
            } else {
                toast("清理失败，请重新清理")
            }
        }
        tv_cache_size.text = CleanDataUtils.getTotalCacheSize(this)
    }

    companion object {
        fun clearUserInfo() {
            try {
                val isYinsi=SP.ISYinsi.get()
                SPUtils.init().clear()
                SP.ISYinsi.put(isYinsi)
                for (i in app.activityStack.size - 1 downTo 0) {
                    if (app.activityStack[i] !is LoginPassActivity) {
                        app.activityStack[i].finish()
                    }
                }
                app.activityTop.startActivity<LoginPassActivity> { }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }
}